﻿using System;
using System.IO;

namespace MyConsoleApp.Code
{
    public class DataToQRBarImage
    {
        public string CureNo { get; set; }
        public string UserCode { get; set; }
        public string DataToImage { get; set; }
    }

    //二维码生成
    public class DotNetBarcodeDemo
    {
        public static dynamic GetQRBarImage(DataToQRBarImage imageData)
        {
            if (string.IsNullOrEmpty(imageData.DataToImage))
                return new { resultCode = "0", resultMsg = "" };

            var imgName = string.Format("{0}_{1}_{2}.bmp", Guid.NewGuid().ToString(), imageData.CureNo, imageData.UserCode);
            var apiFolderName = "qrbarimage";
            var folderPath = System.Web.HttpContext.Current.Server.MapPath("").ToLower();
            folderPath = folderPath.Substring(0, folderPath.LastIndexOf(apiFolderName) + apiFolderName.Length);
            var url = Path.Combine(folderPath, imgName);
            if (!Directory.Exists(folderPath))
            {
                Directory.CreateDirectory(folderPath);
            }

            DotNetBarcode barCode = new DotNetBarcode();
            barCode.Type = DotNetBarcode.Types.QRCode;
            barCode.FontName = "";
            barCode.PrintChar = false;

            barCode.QRSave(imageData.DataToImage, url, 2);
            return new { resultCode = "1", resultMsg = imgName };
        }
    }
}
